package TextNor.Initial;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 */
public final class NorPunc {

    private int url = 0;
    private int money = 0;
    private int num = 0;
    private int seqn = 0;

//    private  Pattern URL_NUM_MONY_NAME = Pattern.compile("(((www)|(http)|(ftp)|(file))[^ ]*)|([^ ]*.net)([^ ]*.org)|([^ ]*.vn)|([^ ]*.com)|((\\d[\\d\\.,\\p{Pd} ]*)?\\d[ ]?((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(\\$)|(S\\$)))|(((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(\\$)|(S\\$))[ ]?(\\d[\\d\\.,\\p{Pd} ]*)?\\d)|([\\d]+,[\\d]+)|([^ ]+@[^ ]+)|(((\\(\\d+\\))|(\\d))[\\d\\.\\p{Pd} :/\\)]*\\d)|((\\p{Lu}\\.)+(\\p{Lu}))");
    public NorPunc(String inF, String ouF) throws XMLStreamException, FileNotFoundException, IOException {
        long startTime = System.currentTimeMillis();

        System.out.println("package TextNor.Initial; class NorPunc");


        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);
        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);
        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);
                    }

                    if (localName.equals("URL")) {
                        String Url = xmlStreamReader.getElementText();
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(Url);
                        xmlStreamWriter.writeEndElement();
                    }
                    if (localName.equals("Infomation")) {
                        String news = xmlStreamReader.getElementText();

                        String newsGroup = norPunc(news);


                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(newsGroup);
                        xmlStreamWriter.writeEndElement();
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();
                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();
                    }

                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    break;

                default:
                    break;
            }
        }
        xmlStreamWriter.writeEndElement();
        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian chay NorPunc: " + time);
        System.out.println("url: " + url);
        System.out.println("money: " + money);
        System.out.println("num: " + num);
        System.out.println("seqn: " + seqn);

    }

    public String norPuncNor(String news) {
//      Pattern PUNC1 = Pattern.compile("(\\.+)|(\\?)|(!)|(\\,)|(;)|(:)|(\\()|(\\))|(\\[)|(\\])|(\')|(\")|(<=?)|(>=?)|(=)");

        Pattern PUNC1 = Pattern.compile("(\\.+)|([\\p{P}\\+&&[^\\-/\\&]])");
        // regex \.+|\p{P}\+&&^\-/\&
        //nhận ra dấu chấm "." >= 1 lần hoặc loại dấu nào đó trừ dấu - / &

//        Pattern PUNC2 = Pattern.compile("/");
//        Pattern PUNC3=Pattern.compile("\\d[ ]+\\p{Pd}[ ]+\\d");

        String newsCopy = "";
        int start = 0;
        int end = 0;
        String found = "";
        int lastIndex = 0;

        Matcher m = PUNC1.matcher(news);
        while (m.find()) {
            start = m.start();
            end = m.end();
            found = m.group();
            found = " " + found + " ";
            newsCopy = newsCopy + news.substring(lastIndex, start) + found;
            lastIndex = end;
        }

        if (lastIndex < news.length()) {
            newsCopy += news.substring(lastIndex);
        }

//        news=newsCpy;
//        newsCpy="";
//        m = PUNC3.matcher(news);
//        lastIndex=0;
//        while (m.find()) {
//            start = m.start();
//            end = m.end();
//            found = m.group();
//            found=found.replace(" ", "");
//            newsCpy = newsCpy + news.substring(lastIndex, start) + found;
//            lastIndex = end;"
//        }
//
//        if (lastIndex < news.length()) {
//            newsCpy += news.substring(lastIndex);
//        }
        newsCopy.replaceAll("[ ]*/[ ]*", "/");
        newsCopy.replaceAll("[ ]*", " ");
        System.out.println("kakaka"+newsCopy);
        return newsCopy;
    }

    public String norPunc(String news) {// nhận ra số, tiền, url, dãy?
//     Pattern URL_NUM_MONY_NAME = Pattern.compile("(\\b((www)|(\\bhttp)|(\\bftp)|(\\bfile))[^ \\),;]*)|([^ ]*.net)([^ ]*.org)|([^ ]*.vn)|([^ ]*.com)|([^ ]+@[^ ]+)|(\\b(\\d[\\d\\.,\\p{Pd} ]*)?\\d[ ]?((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(đ\\b)|(đồng\\b)|(\\$)|(S\\$)))|(\\b((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(\\$)|(S\\$))[ ]?(\\d[\\d\\.,\\p{Pd} ]*)?\\d)|(\\b([\\d]+:)+\\d+)|([\\d]+[,][\\d]+([ ]?\\p{Pd}[ ]?[\\d]+([,]?[\\d]+)?)?)|(\\b((\\(\\d+\\))|(\\d))[\\d\\.\\p{Pd} /\\)]*\\d)|(\\b(\\p{Lu}\\.)+(\\p{Lu}))");
        Pattern URL_NUM_MONY_NAME = Pattern.compile("(\\b((www)|(\\bhttp)|(\\bftp)|(\\bfile))[^ \\),;]*)|([^ ]*.net)([^ ]*.org)|([^ ]*.vn)|([^ ]*.com)|([^ ]+@[^ ]+)|(\\b(\\d[\\d\\.,\\p{Pd} ]*)?\\d[ ]?((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(đ\\b)|(đồng\\b)|(\\$)|(S\\$)))|(\\b((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(\\$)|(S\\$))[ ]?(\\d[\\d\\.,\\p{Pd} ]*)?\\d)|([\\d]+[,][\\d]+([ ]?\\p{Pd}[ ]?[\\d]+([,]?[\\d]+)?)?)|(\\b((\\(\\d+\\))|(\\d))[\\d\\.\\p{Pd} :/\\)]*\\d)|(\\b(\\p{Lu}\\.)+(\\p{Lu}))");
        // có cụm www hoặc http hoặc ftp hoặc file mà không có dấu cách, dấu đóng ngoặc ), dấu , và dấu ; hoặc không có dấu cách trắng trước cụm .net, .org, .vn, .com, @
        Pattern URL = Pattern.compile("(((\\bwww)|(\\bhttp)|(\\bftp)|(\\bfile))[^ \\),;]*)|([^ ]*.net)([^ ]*.org)|([^ ]*.vn)|([^ ]*.com)|([^ ]+@[^ ]+)");
        Pattern MONEY = Pattern.compile("((\\d[\\d\\.,\\p{Pd} ]*)?\\d[ ]?((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(đ\\b)|(đồng\\b)|(\\$)|(S\\$)))|(((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(\\$)|(S\\$))[ ]?(\\d[\\d\\.,\\p{Pd} ]*)?\\d)");
        Pattern NUM = Pattern.compile("([\\d]+,[\\d]+)|( ((\\(\\d+\\))|(\\d))[\\d\\.\\p{Pd} :/\\)]*\\d)");
        Pattern SEQN = Pattern.compile("((\\p{Lu}\\.)+(\\p{Lu}))");

        //   Pattern MONY = Pattern.compile("((\\d[\\d\\., ]*)?\\d[ ]?((AUD)|(SGD)|(USD)|(VNĐ)|(Đ)|(\\$)|(S\\$)))");
        String newsCpy = "";
        int start = 0;
        int end = 0;
        String found = "";
        int lastIndex = 0;
        lastIndex = 0;
        String lastString = "";

        Matcher m = URL_NUM_MONY_NAME.matcher(news);
        while (m.find()) {
            found = m.group();

            Matcher mu = URL.matcher(found);
            if (mu.find()) {
                url++; // tìm thấy url
            } else {
                Matcher mm = MONEY.matcher(found);
                if (mm.find()) {
                    money++;//tìm thấy tiền
                } else {
                    Matcher mn = NUM.matcher(found);
                    if (mn.find()) {
                        num++; //tìm thấy số
                    } else {
                        seqn++;// tìm thấy dãy
                    }
                }
            }
            start = m.start();
            end = m.end();
//            found=found.replace(" ", "");
            String subfound = found.substring(1, found.length());
            subfound = subfound.replace(" ", "");
            found = found.substring(0, 1) + subfound;
            lastString = news.substring(lastIndex, start);
            lastString = norPuncNor(lastString);
            newsCpy += lastString + found;
            lastIndex = end;
        }
        if (lastIndex < news.length()) {
            lastString = news.substring(lastIndex);
            lastString = norPuncNor(lastString);
            newsCpy += lastString;
        }

        return newsCpy;
    }
}
